/*********************************************************************
*
* Software License Agreement (BSD License)
*
*  Copyright (c) 2010, Heriot-Watt University, UK.
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the Heriot-Watt University nor the names of
*     its contributors may be used to endorse or promote products
*     derived from this software without specific prior written
*     permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*
*  Author: Joel Cartwright, Nick Johnson
*
*********************************************************************/

#ifndef _AUV_PILOT_PILOT_H_
#define _AUV_PILOT_PILOT_H_

#include <sstream>

#include <osl_core/ConfigWrapper.h>
#include <osl_core/TimerMillis.h>

#include <auv_pilot/pid.h>

namespace auv_pilot
{

template<class T>
class BodyFrame
{
public:
	BodyFrame()
	{
	};

	BodyFrame(T x, T y, T z, T roll, T pitch, T yaw)
	{
		set(x, y, z, roll, pitch, yaw);
	};


	void set(T x, T y, T z, T roll, T pitch, T yaw)
	{
		this->x = x;
		this->y = y;
		this->z = z;
		this->roll = roll;
		this->pitch = pitch;
		this->yaw = yaw;
	}

	T x, y, z, roll, pitch, yaw;
};

template<class T>
class BodyFrameNumeric
{
public:
	BodyFrameNumeric()
	{
		reset();
	};

	BodyFrameNumeric(T x, T y, T z, T roll, T pitch, T yaw)
	{
		set(x, y, z, roll, pitch, yaw);
	};


	void set(T x, T y, T z, T roll, T pitch, T yaw)
	{
		this->x = x;
		this->y = y;
		this->z = z;
		this->roll = roll;
		this->pitch = pitch;
		this->yaw = yaw;
	}

	void reset()
	{
		set(0, 0, 0, 0, 0, 0);
	}

	T x, y, z, roll, pitch, yaw;
};


template<class T>
class NEDARPY
{
public:
	NEDARPY()
	{
		reset();
	};

	void reset()
	{
		n = 0;
		e = 0;
		d = 0;
		a = 0;
		roll = 0;
		pitch = 0;
		yaw = 0;
	}

	NEDARPY(T n, T e, T d, T a, T roll, T pitch, T yaw)
	{
		set(n, e, d, a, roll, pitch, yaw);
	}

	void set(T n, T e, T d, T a, T roll, T pitch, T yaw)
	{
		this->n = n;
		this->e = e;
		this->d = d;
		this->a = a;
		this->roll = roll;
		this->pitch = pitch;
		this->yaw = yaw;
	}

	T n;
	T e;
	T d;
	T a;
	T roll;
	T pitch;
	T yaw;
};

class Waypoint
{
public:
	Waypoint()
	{
		reset();
	}

	void set(const std::string& requester, uint32_t id, uint32 priority)
	{
		this->requester = requester;
		this->id = id;
		this->priority = priority;
	}

	void reset()
	{
		requester = "";
		id = 0;
		priority = 0;
	}

	std::string toString()
	{
		std::stringstream ss;
		ss << '[' << requester << ", id " << id << ", pri " << priority << ']';
		return ss.str();
	}

	std::string requester;
	uint32_t id;
	uint32_t priority;

};

class Pilot
{
public:
	Pilot();
	virtual ~Pilot();

    bool init(ros::NodeHandlePtr rosHandle);

	void resetControl();

	void resetControlVelocity();

	bool readPIDParams();

	bool updatePIDParams();

	bool writePIDParams();

	void printDiag();

	void setWorldPose(double north, double east, double depth, double altitude, double roll, double pitch, double yaw);

	void setBodyVelocity(double x, double y, double z, double roll, double pitch, double yaw);

	bool requestWorldWaypoint(const std::string& requester, uint32_t goalID, uint32_t priority,
			double north, double east, double depth, double altitude, bool depthMode,
			double pitch, double yaw,
			double xTol, double yTol, double zTol, double pitchTol, double yawTol,
			bool xEnable, bool yEnable, bool zEnable, bool pitchEnable, bool yawEnable);

	bool requestBodyWaypoint(const std::string& requester, uint32_t goalID, uint32_t priority,
			double x, double y, double z, bool depthMode, double pitch, double yaw,
			double xTol, double yTol, double zTol, double pitchTol, double yawTol,
			bool xEnable, bool yEnable, bool zEnable, bool pitchEnable, bool yawEnable);

	bool requestStayWaypoint(const std::string& requester, uint32_t goalID, uint32_t priority, bool depthMode,
			double xTol, double yTol, double zTol, double pitchTol, double yawTol,
			bool xEnable, bool yEnable, bool zEnable, bool pitchEnable, bool yawEnable);

	void manualForceRequest(double x, double y, double z, double pitch, double yaw);

	void velocityRequest(double x, double y, double z, double pitch, double yaw,
			bool xEnable, bool yEnable, bool zEnable, bool pitchEnable, bool yawEnable);
	
	void update(double deltaSec);

	void updateVelocity(double deltaSec);

	inline bool inPositionX() { return !axisEnabled_.x || fabs(poseError_.x) < requestTol_.x; }
	inline bool inPositionY() { return !axisEnabled_.y || fabs(poseError_.y) < requestTol_.y; }
	inline bool inPositionZ() { return !axisEnabled_.z || fabs(poseError_.z) < requestTol_.z; }
	inline bool inPositionPitch() { return !axisEnabled_.pitch || fabs(poseError_.pitch) < requestTol_.pitch; }
	inline bool inPositionYaw() { return !axisEnabled_.yaw || fabs(poseError_.yaw) < requestTol_.yaw; }

	bool inPosition();

	void getAxisInPosition(bool &x, bool &y, bool &z, bool &pitch, bool &yaw);

	const BodyFrameNumeric<double>& getPoseError();

	void getCurrentWaypoint(std::string &requester, uint32_t &id, uint32_t &priority);

	void getAxisEnables(bool &x, bool &y, bool &z, bool &pitch, bool &yaw);

	void getRequestedAxisForces(double &x, double &y, double &z, double &pitch, double &yaw);
	
	void getRequestedAxisVelocities(double &x, double &y, double &z, double &pitch, double &yaw);

private:
	bool checkAndSetNewWaypoint(const std::string& requester, uint32_t goalID, uint32_t priority);

	void setStaleWaypointTimer();

	bool initialised_;

    osl_core::ConfigWrapper conf_;

    bool receivedWorldPose_;

    double minAltitude_;

    NEDARPY<double> currentPose_;
	BodyFrameNumeric<double> currentVel_;

    Waypoint currentWaypoint_;
    osl_core::TimerMillis staleWaypointTimer_;
    timeout_t staleWaypointTimeout_;

    NEDARPY<double> requestPose_;
    bool requestDepthMode_;


	BodyFrameNumeric<double> requestTol_;
	BodyFrameNumeric<bool> axisEnabled_;
	BodyFrameNumeric<bool> axisEnabledVel_;

	BodyFrameNumeric<double> poseError_;
	BodyFrame<Pid> posePID_;
	BodyFrameNumeric<double> requestVel_;
	BodyFrameNumeric<double> requestVelMode_;

	BodyFrameNumeric<double> velError_;
	BodyFrame<Pid> velPID_;
	BodyFrameNumeric<double> requestForce_;

	BodyFrameNumeric<float> velLimit_;
};


} // namespace

#endif
